﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Framework.Noebe;
using Pixysoft.SQL;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Weblications.Sango.Dao
{
    class UsrSectionBattleDao
    {
        /// <summary>
        /// 创建玩家记录
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="stage"></param>
        /// <param name="sectionserialcode"></param>
        /// <param name="soulvalue"></param>
        /// <param name="equipmentserialcode"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static Entities.Usr_Sectionbattle CreateSectionBattle(Entities.Usr_Profile profile,
            long sectionserialcode, long targetsectionserialcode, long famevalue, bool result)
        {
            Entities.Usr_Sectionbattle orm = Entities.Local.Usr_Sectionbattle;
            orm.Primarycode = PkHelper.PrimaryCode();
            orm.Usercode = profile.Usercode;
            orm.Nickname = profile.Nickname;

            orm.Sectionserialcode = sectionserialcode;
            orm.Targetsectionserialcode = targetsectionserialcode;
            orm.Fightdate = GlobalTimer.Instance.ToTimeSequence();
            orm.Fightresult = SettingHelper.IntValue(result);
            orm.Battlestatus = GeneralStatusCollection.OPEN;

            orm.Famevalue = famevalue;
            return orm;
        }

        /// <summary>
        /// 获取玩家的战斗记录
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public static List<Entities.Usr_Sectionbattle> GetSectionBattles(string usercode)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = SqlCreator.SelectAll().From(Schemas.LOCAL.USR_SECTIONBATTLE)
            .Where(Schemas.LOCAL.USR_SECTIONBATTLE.USERCODE).Equal("USERCODE", true)
            .And(Schemas.LOCAL.USR_SECTIONBATTLE.BATTLESTATUS).Equal("STATUS", true).GetSQL();

            command.Parameters.Add("USERCODE", usercode);
            command.Parameters.Add("STATUS", GeneralStatusCollection.OPEN);
            return command.ExecuteReader<Entities.Usr_Sectionbattle>();
        }

        /// <summary>
        /// 获取战报数量
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public static int GetSectionBattlesCount(string usercode)
        {
            INoebeCommand command = SettingHelper.GetNoebe().NoebeCommand;
            command.SQL = SqlCreator.Select("COUNT(*)").From(Schemas.LOCAL.USR_SECTIONBATTLE)
            .Where(Schemas.LOCAL.USR_SECTIONBATTLE.USERCODE).Equal("USERCODE", true)
            .And(Schemas.LOCAL.USR_SECTIONBATTLE.BATTLESTATUS).Equal("STATUS", true).GetSQL();

            command.Parameters.Add("USERCODE", usercode);
            command.Parameters.Add("STATUS", GeneralStatusCollection.OPEN);
            return int.Parse(command.ExecuteReader().Rows[0][0].ToString());
        }

        /// <summary>
        /// 根据主键查询 
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Entities.Usr_Sectionbattle GetSectionBattle(string code)
        {
            Entities.Usr_Sectionbattle orm = SettingHelper.GetNoebe().EntityNoebeCommand.SelectByPk<Entities.Usr_Sectionbattle>(code);

            if (orm == null)
                throw Exceptions.VerificationFailedException(code);

            return orm;
        }
    }
}
